package com.tanhua.server.service;

import com.tanhua.dubbo.api.db.BlackListApi;
import com.tanhua.dubbo.api.db.QuestionApi;
import com.tanhua.dubbo.api.db.SettingsApi;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.model.db.*;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVoAge;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通用设置业务处理层
 */
@Service
@Slf4j
public class SettingsService {

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private BlackListApi blackListApi;

    @DubboReference
    private UserInfoApi userInfoApi;


    /**
     * 用户通用设置 - 读取
     */
    public SettingsVo findSettings() {
        User user = UserHolder.getUser();
        Long userId = user.getId();
        //1.根据当前用户id查询tb_question表 获取陌生人问题
        Question question = questionApi.findQuestionById(userId);
        //1.1如果问题不存在，默认问题(SettingsVo设置了默认值)
        //2.根据当前用户id查询tb_settings表 获取3个通知设置值
        Settings settings = settingsApi.findSettingsById(userId);
        //3.从UserHolder.getUser().getMobile()获取手机号
        String mobile = user.getMobile();
        //4.构造SettingsVo 返回
        SettingsVo settingsVo = new SettingsVo();
        settingsVo.setPhone(mobile);
        if(question != null){
            settingsVo.setStrangerQuestion(question.getTxt());
        }
        if(settings != null){
            BeanUtils.copyProperties(settings,settingsVo);
        }
        return settingsVo;
    }

    /**
     * 设置陌生人问题 - 保存
     */
    public void addOrUpdateQuestions(String content) {
        Question question = new Question();
        question.setUserId(UserHolder.getUserId());//当前用户id
        question.setTxt(content);//陌生人问题
        questionApi.addOrUpdateQuestions(question);
    }

    /**
     * 通知设置 - 保存
     */
    public void addOrUpdateSettings(SettingsVo settingsVo) {
        Settings settings = new Settings();
        BeanUtils.copyProperties(settingsVo,settings);
        settings.setUserId(UserHolder.getUserId());
        settingsApi.addOrUpdateSettings(settings);
    }

    /**
     * 黑名单 - 分页列表
     */
    public PageResult<UserInfoVoAge> findPageBlacklist(Long page, Long pageSize) {
        Long userId = UserHolder.getUserId();
        //1 根据当前用户id分页查询黑名单表
        PageResult<BlackList> pageResult = blackListApi.findPageBlacklist(page,pageSize,userId);
        if(CollectionUtils.isEmpty(pageResult.getItems())){
            return null;
            //return new PageResult<>(page,pageSize,0l,null);
        }
        //2 获取出黑名单用户ids
        List<BlackList> blackLists = pageResult.getItems();
        //方式一：
        List<Long> blackUserIds = blackLists.stream().map(BlackList::getBlackUserId).collect(Collectors.toList());//1,2,3,4,5
        //方式二：
        /*List<Long> blackUserIds = new ArrayList<>();
        for (BlackList blackList : blackLists) {
            blackUserIds.add(blackList.getBlackUserId());
        }*/
        //3 根据黑名单用户ids查询tb_user_info表
        List<UserInfo> userInfoList = userInfoApi.findUsersByBatchIds(blackUserIds);
        //3.1 将List<UserInfo> 转换为 List<UserInfoVoAge>
        List<UserInfoVoAge> userInfoVoAgeList = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            UserInfoVoAge userInfoVoAge = new UserInfoVoAge();
            BeanUtils.copyProperties(userInfo,userInfoVoAge);
            userInfoVoAge.setId(userInfo.getId());//黑名单用户id
            userInfoVoAgeList.add(userInfoVoAge);
        }
        //4返回PageResult<UserInfoVoAge>
        return new PageResult<>(page,pageSize,pageResult.getCounts(),userInfoVoAgeList);
    }


    /**
     * 黑名单 - 移除
     * uid:黑名单用户id
     */
    public void deleteBlacklistById(Long blackUserId) {
        blackListApi.deleteBlacklistById(blackUserId,UserHolder.getUserId());
    }
}
